//
//  RemoteControlViewController.m
//  wAudioShare
//
//  Created by 赵帅 on 13-12-12.
//  Copyright (c) 2013年 赵帅. All rights reserved.
//

#import "RemoteControlViewController.h"
#import "WISInterfaces.h"
#import "wis.h"

@interface RemoteControlViewController ()
{
    BOOL isAlreadyPop;
}
@property (assign) IBOutlet UITextField * textField;

@end

@implementation RemoteControlViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRemove:) name:@"PopRemoteControl" object:nil];
    
    isAlreadyPop = NO;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - navigation methods
-(NSString *)navigationBarTitle
{
    return @"WIS Demo";
}

#pragma mark - IBAction methods
-(IBAction)lightOnButtonPressed:(id)sender
{
    //[WISInterfaces wis_send_private_cmd:[GlobalInfo sharedInstance].m_CmdSock cmdId:WIS_CMD_TX_LED_ON data:nil len:0 uuid:(char *)[_uuid UTF8String]];
    
   
    [WISInterfaces wis_send_private_cmd:[GlobalInfo sharedInstance].m_CmdSock cmdId:WIS_CMD_V2_DEV_LIST_GET data: (void *)[[[GlobalInfo sharedInstance] getUniqueID] UTF8String] len:32 uuid:(char *)[_uuid UTF8String]];
    
}

-(IBAction)lightOffButtonPressed:(id)sender
{
    [WISInterfaces wis_send_private_cmd:[GlobalInfo sharedInstance].m_CmdSock cmdId:WIS_CMD_TX_LED_OFF data:nil len:0 uuid:(char *)[_uuid UTF8String]];
}

-(IBAction)sendText:(id)sender
{
    NSString * text = _textField.text;
    
    if([text length] != 0)
    {
        NSData* aData = [text dataUsingEncoding: NSASCIIStringEncoding];
        
        [WISInterfaces wis_send_uart_cmd:[GlobalInfo sharedInstance].m_CmdSock text:(char *)[aData bytes] textLength:[text length] uuid:(char *)[_uuid UTF8String]];
    }
}

-(void)deviceRemove:(NSNotification *)notification
{
    if([notification.object isEqualToString:self.uuid])
    {
        if(isAlreadyPop == NO)
        {
            isAlreadyPop = YES;
            
            if([_delegate respondsToSelector:@selector(clearAllDevices)])
            {
                [_delegate performSelector:@selector(clearAllDevices) withObject:nil];
            }
            
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

@end
